package dataSystem;

import java.util.Map;
import java.util.HashMap;

public class ReportGenerator {
    private DataIntegrationCenter dataCenter;
    
    public ReportGenerator(DataIntegrationCenter dataCenter) {
        this.dataCenter = dataCenter;
    }
    
    public InventoryReport generateInventoryReport() {
        DataIntegrationCenter.IntegratedData data = dataCenter.integrateData();
        
        // 计算总销售量
        int salesVolume = data.getSalesData().stream()
            .flatMap(order -> order.getItems().stream())
            .mapToInt(OrderItem::getQuantity)
            .sum();
        
        // 计算平均库存量（简化版）
        double avgInventory = data.getPurchaseData().stream()
            .mapToInt(PurchaseOrder::getQuantity)
            .average()
            .orElse(0.0);
        
        // 计算库存周转率
        double turnoverRate = salesVolume / (avgInventory == 0 ? 1 : avgInventory);
        
        return new InventoryReport(turnoverRate, salesVolume, avgInventory);
    }
    
    public SupplierReport generateSupplierReport() {
        DataIntegrationCenter.IntegratedData data = dataCenter.integrateData();
        Map<String, SupplierPerformance> performanceMap = new HashMap<>();
        
        for (PurchaseOrder order : data.getPurchaseData()) {
            String supplierId = order.getSupplierId();
            SupplierPerformance performance = performanceMap.computeIfAbsent(supplierId, 
                k -> new SupplierPerformance(supplierId));
            
            performance.incrementTotalOrders();
            if (order.isDeliveredOnTime()) {
                performance.incrementOnTimeDeliveries();
            }
            if (order.isHasQualityIssues()) {
                performance.incrementQualityIssues();
            }
        }
        
        // 计算绩效评分
        performanceMap.values().forEach(SupplierPerformance::calculateScore);
        
        return new SupplierReport(performanceMap);
    }
    
    // 库存报告类
    public static class InventoryReport {
        private double turnoverRate;
        private int salesVolume;
        private double averageInventory;
        
        public InventoryReport(double turnoverRate, int salesVolume, double averageInventory) {
            this.turnoverRate = turnoverRate;
            this.salesVolume = salesVolume;
            this.averageInventory = averageInventory;
        }
        
        public double getTurnoverRate() { return turnoverRate; }
        public int getSalesVolume() { return salesVolume; }
        public double getAverageInventory() { return averageInventory; }
    }
    
    // 供应商报告类
    public static class SupplierReport {
        private Map<String, SupplierPerformance> supplierPerformance;
        
        public SupplierReport(Map<String, SupplierPerformance> supplierPerformance) {
            this.supplierPerformance = supplierPerformance;
        }
        
        public Map<String, SupplierPerformance> getSupplierPerformance() {
            return supplierPerformance;
        }
    }
    
    // 供应商绩效类
    public static class SupplierPerformance {
        private String supplierId;
        private int totalOrders;
        private int onTimeDeliveries;
        private int qualityIssues;
        private double performanceScore;
        
        public SupplierPerformance(String supplierId) {
            this.supplierId = supplierId;
        }
        
        public void incrementTotalOrders() { totalOrders++; }
        public void incrementOnTimeDeliveries() { onTimeDeliveries++; }
        public void incrementQualityIssues() { qualityIssues++; }
        
        public void calculateScore() {
            double deliveryScore = ((double)onTimeDeliveries / totalOrders) * 50;
            double qualityScore = 50 - ((double)qualityIssues / totalOrders) * 50;
            performanceScore = deliveryScore + qualityScore;
        }
        
        public String getSupplierId() { return supplierId; }
        public int getTotalOrders() { return totalOrders; }
        public int getOnTimeDeliveries() { return onTimeDeliveries; }
        public int getQualityIssues() { return qualityIssues; }
        public double getPerformanceScore() { return performanceScore; }
    }
}